import request from '@/utils/request'
import { useEffect, useState } from 'react'

// 获取新手任务
export function useNewTask() {
  const [task, setTask] = useState({
    createTime: '',
    description: '',
    id: 0,
    taskCategoryId: 0,
    taskCategoryName: '',
    timeMoney: 0,
    title: '',
    updateTime: '',
    weight: 0,
  })
  const [loading, setLoading] = useState(false)

  useEffect(() => {
    setLoading(true)

    const fetchData = async () => {
      const result = await request.get('/api/task/getCurrent')
      setTask(result)
      setLoading(false)
    }

    fetchData()
  }, [])

  return { loading, task, setTask }
}

// 昨日统计
export function useYesterday() {
  const [count, setCount] = useState({
    id: 0,
    forwardingOrderNum: 0,
    rightsOrderProtectionNum: 0,
    yesterdayOrderNum: 0,
    yesterdayTransactionAmount: '0.00',
    withdrawalBalance: '0.00',
    storeId: 0,
    createTime: '',
    updateTime: '',
  })
  const [loading, setLoading] = useState(false)

  useEffect(() => {
    setLoading(true)

    const fetchData = async () => {
      const result = await request.get('/api/order/statical/yesterday')
      setCount(result)
      setLoading(false)
    }

    fetchData()
  }, [])

  return { loading, count, setCount }
}

// 新闻列表
export function useNews() {
  const [newsData, setNewsData] = useState([
    {
      categoryId: 0,
      categoryName: '',
      news: [
        {
          id: 0,
          title: '',
          linkUrl: '',
        },
      ],
    },
  ])
  const [loading, setLoading] = useState(false)

  useEffect(() => {
    setLoading(true)

    const fetchData = async () => {
      const result = await request.get('/api/news/getHomeRightNews')
      setNewsData(result)
      setLoading(false)
    }

    fetchData()
  }, [])

  return { loading, newsData, setNewsData }
}
